const redis = require("../middleware/redis");
const config = require('../../config/config');
class RedisModules {
    //读取string
    static async get(redisName){
        let data;
        await redis.get(config.redis.prefix +redisName).then(function (result) {
            data = JSON.parse(result);
        }).catch(function (error) {
            console.log(error);
        });
        return data;
    }
    //写入redis
    static async set(redisName,data,ex='EX',time=30){
        return await redis.set(config.redis.prefix +redisName, data, ex, time);
    }
    //删除
    static async del(redisName){
        return await redis.del(config.redis.prefix +redisName);
    }
    //存储hash
    static async hmset(redisName, data) {
        return await redis.hmset(config.redis.prefix + redisName, data);
    }
    //查找hash单个值
    static async hget(redisName, key) {
        let data = '';
        await redis.hget(config.redis.prefix + redisName, key).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        ;
        return data;
    }
    //查找hash全部值
    static async hgetAll(redisName) {
        let data = {};
        await redis.hgetall(config.redis.prefix + redisName).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        return data;
    }
    //更新一个hash值
    static async hset(redisName, key, value) {
        let data = {};
        await redis.hset(config.redis.prefix + redisName, key, value).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        return data;
    }
    //为hash中整数增加值
    static async hincrby(redisName, key, number) {
        let data = {};
        await redis.hincrby(config.redis.prefix + redisName, key, number).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        return data;
    }
    //判断hash中某个值是否存在
    static async hexists(redisName, key) {
        let data = 0;
        await redis.hexists(config.redis.prefix + redisName, key).then(function (result) {
            data = !result ? 0 : result;
        }).catch(function (error) {
            console.log(error);
        });
        return data;
    }
    //向sort中插入数据
    static async zadd(redisName, data) {
        return await redis.zadd(config.redis.prefix + redisName, data);
    }
    //获取有序集合成员数
    static async zcard(redisName){
        return await redis.zcard(config.redis.prefix + redisName);
    }
    //返回有序集中指定区间内的成员，通过索引，分数从高到低
    static async zrevrange(redisName,min,max){
        let data;
        await redis.zrevrange(config.redis.prefix + redisName,min,max).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        return data
    }
    //移除有序集合中的一个或多个成员
    static async zrem(redisName,member){
        return await redis.zrem(config.redis.prefix + redisName,member);
    }
    //判断成员是否存在
    static async zrank(redisName,member){
        let data=-1;
        await redis.zrank(config.redis.prefix + redisName,member).then(function (result) {
            data = result==null?-1:result;
        }).catch(function (error) {
            console.log(error);
        });
        return data
    }
    static async zrange(redisName,min,max){
        let data;
        await redis.zrange(config.redis.prefix + redisName,min,max).then(function (result) {
            data = result;
        }).catch(function (error) {
            console.log(error);
        });
        return data
    }
}
module.exports = RedisModules;